# -*- coding: utf-8 -*-
# 车内件流行
# author： 郭瑞玲
# create_time: 2022-05-13

from datetime import timedelta
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from jms_hi.dim.dim_mysql_lmdm_sys_first_code import jms_dim__dim_mysql_lmdm_sys_first_code
from jms_hi.dim.dim_mysql_lmdm_sys_second_code import jms_dim__dim_mysql_lmdm_sys_second_code
#from jms_hi.dm.dm_route_recommend import jms_dm__dm_route_recommend
#from jms_hi.dwd.dwd_wide_tms_road_waybill_hf import jms_dwd__dwd_wide_tms_road_waybill_hf
from jms_hi.dim.dim_network_whole_massage import jms_dim__dim_network_whole_massage
from jms_hi.dm.dm_shipment_stop_billcode_detail_dt import jms_hi_dm__dm_shipment_stop_billcode_detail_dt
# 车内件流向明细
jms_hi_dm__dm_truck_wabill_flow_detail_ht = SparkSqlOperator(
    task_id='jms_hi_dm__dm_truck_wabill_flow_detail_ht',
    task_concurrency=1,
    depends_on_past=True,
    pool_slots=2,
    master='yarn',
    execution_timeout=timedelta(hours=2),
    sla=timedelta(minutes=120),
    email=['guoruiling@jtexpress.com','yl_bigdata@yl-scm.com'],
    name='jms_hi_dm__dm_truck_wabill_flow_detail_ht_{{ execution_date | cst_hour }}',
    sql='jms_hi/dm/dm_truck_wabill_flow_detail_ht/execute.sql',
    driver_memory='8G',
    driver_cores=4,
    executor_cores=6,
    executor_memory='10G',
    num_executors=10,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 30,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.sql.shuffle.partitions': 360,
          'spark.default.paralleism': 200,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.network.timeout': 900,
          'spark.core.connection.ack.wait.timeout': 300,
          'spark.sql.autoBroadcastJoinThreshold': 104857600,
          #'spark.yarn.executor.memoryOverhead': 8192,
          'spark.shuffle.consolidateFiles': 'true',
          'spark.shuffle.memoryFraction': '0.8',
          'spark.shuffle.file.buffer':'64k',
          'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=6 -XX:ConcGCThreads=2'
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 200,  # 每天生成 20 个分区
              'hive.exec.max.dynamic.partitions.pernode': 200,  # 每天生成 20 个分区
              },
    yarn_queue='pro',
    retries=2,
)


jms_hi_dm__dm_truck_wabill_flow_detail_ht << [
    jms_dim__dim_mysql_lmdm_sys_second_code,
    jms_dim__dim_mysql_lmdm_sys_first_code,
    #jms_dwd__dwd_wide_tms_road_waybill_hf,
    jms_dim__dim_network_whole_massage,
    jms_hi_dm__dm_shipment_stop_billcode_detail_dt
]
